/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows;
using _videoConference.ZipUnzip;
using System.Collections;
using System.Reflection;

namespace _videoConference.BAL
{
    public class ZipUnzip
    {
        _videoConference.DAL.Database d = new _videoConference.DAL.Database();
        IAddPage ia = new IAddPage();
        string finaldestination;
        ArrayList al = new ArrayList();
        ArrayList al1 = new ArrayList();
        System.Reflection.Assembly a;
        AppDomain appDomain;

        public string uploadfile(string filepath, string modulename)
        {
            try
            {
                if (!File.Exists(filepath))
                {
                    MessageBox.Show("Specify the file to upload");
                    return "";
                }
                string[] FileName = filepath.Split('\\');
                string[] zipname = FileName[FileName.Length - 1].ToString().Split('.');
                System.Reflection.Assembly ass = System.Reflection.Assembly.GetAssembly(typeof(ZipUnzip));

                string filezip = ass.Location.Replace("1videoConference.BAL.dll", @"Zip Files");
                string filesource = filezip + "\\" + FileName[FileName.Length - 1].ToString();
                string filetemp = ass.Location.Replace("1videoConference.BAL.dll", @"Temp");
                string pathview = ass.Location.Replace("1videoConference.BAL.dll", @"Presentation Layer");
                string pathdal = ass.Location.Replace("1videoConference.BAL.dll", @"Data Layer");
                string pathbal = ass.Location.Replace("1videoConference.BAL.dll", @"Business Layer");
                string pathimage = ass.Location.Replace("1videoConference.BAL.dll", @"Image");
                string pathsql = ass.Location.Replace("1videoConference.BAL.dll", @"SQL Scripts");
                string pathziptemp = ass.Location.Replace("1videoConference.BAL.dll", @"Zip Temp");


                if (File.Exists(filesource))
                {
                    MessageBox.Show("Specify other name of zip file");
                    return "";
                }
                else
                {
                    #region if
                    if (Directory.Exists(pathbal + "\\" + modulename))
                    {
                        MessageBox.Show("This Module name already exists in the database.Specify other name");
                        return "";
                    }
                    #endregion

                    #region else

                    else
                    {

                        finaldestination = filezip + "\\" + modulename + ".zip";
                        File.Copy(filepath, filesource);
                        File.Move(filesource, finaldestination);

                        _videoConference.ZipUnzip.Zip.FastZip fz = new _videoConference.ZipUnzip.Zip.FastZip();

                        fz.ExtractZip(finaldestination, filetemp, null);

                        Directory.CreateDirectory(pathview + "\\" + modulename);
                        Directory.CreateDirectory(pathdal + "\\" + modulename);
                        Directory.CreateDirectory(pathbal + "\\" + modulename);
                        System.Security.Permissions.FileIOPermission oPerm = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.Write, pathview + "\\" + modulename);
                        System.Security.Permissions.FileIOPermission oPerm1 = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.Write, pathbal + "\\" + modulename);
                        System.Security.Permissions.FileIOPermission oPerm2 = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.Write, pathdal + "\\" + modulename);

                        string[] dirnameorg = Directory.GetDirectories(filetemp);
                        string[] dirname = Directory.GetDirectories(dirnameorg[0]);


                        for (int i = 0; i < dirname.Length; i++)
                        {
                            string[] strSplit = dirname[i].ToString().Split('\\');

                            if (strSplit[strSplit.Length - 1].ToUpper() == "BAL")
                            {
                                string[] balfilenames = Directory.GetFiles(dirname[i].ToString());
                                for (int j = 0; j < balfilenames.Length; j++)
                                {
                                    string[] filefoldername = balfilenames[j].Split('\\');
                                    File.Copy(balfilenames[j], pathbal + "\\" + modulename + "\\" + filefoldername[filefoldername.Length - 1].ToString());
                                }
                            }

                            if (strSplit[strSplit.Length - 1].ToUpper() == "DAL")
                            {
                                string[] balfilenames = Directory.GetFiles(dirname[i].ToString());
                                for (int j = 0; j < balfilenames.Length; j++)
                                {
                                    string[] filefoldername = balfilenames[j].Split('\\');
                                    File.Copy(balfilenames[j], pathdal + "\\" + modulename + "\\" + filefoldername[filefoldername.Length - 1].ToString());
                                }

                            }

                            if (strSplit[strSplit.Length - 1].ToUpper() == "CONTROL")
                            {
                                string[] balfilenames = Directory.GetFiles(dirname[i].ToString());
                                for (int j = 0; j < balfilenames.Length; j++)
                                {
                                    string[] filefoldername = balfilenames[j].Split('\\');
                                    File.Copy(balfilenames[j], pathview + "\\" + modulename + "\\" + filefoldername[filefoldername.Length - 1].ToString());
                                }

                            }

                            if (strSplit[strSplit.Length - 1].ToUpper() == "IMAGES")
                            {
                                string[] balfilenames = Directory.GetFiles(dirname[i].ToString());
                                for (int j = 0; j < balfilenames.Length; j++)
                                {
                                    string[] filefoldername = balfilenames[j].Split('\\');
                                    File.Copy(balfilenames[j], pathimage + "\\" + modulename + "\\" + filefoldername[filefoldername.Length - 1].ToString());
                                }
                            }

                            if (strSplit[strSplit.Length - 1].ToUpper() == "SQL")
                            {
                                string[] baldirnames = Directory.GetDirectories(dirname[i].ToString());
                                for (int dircount = 0; dircount < baldirnames.Length; dircount++)
                                {
                                    string[] files = baldirnames[dircount].Split('\\');
                                    Directory.CreateDirectory(pathsql + "\\" + modulename + "\\" + files[files.Length - 1].ToString());
                                    string[] sqlfilenames = Directory.GetFiles(baldirnames[dircount]);
                                    for (int j = 0; j < sqlfilenames.Length; j++)
                                    {
                                        string[] filefoldername = sqlfilenames[j].Split('\\');
                                        File.Copy(sqlfilenames[j], pathsql + "\\" + modulename + "\\" + files[files.Length - 1].ToString() + "\\" + filefoldername[filefoldername.Length - 1].ToString());
                                    }
                                }
                            }
                        }
                        Directory.Delete(dirnameorg[0], true);
                    }
                    #endregion

                    string zipflag = ia.addzipinfo(FileName[FileName.Length - 1].ToString(), modulename, finaldestination);
                    //////////////////modulename//////////////
                    if (zipflag != "-1")
                    {
                        DirectoryInfo diview = new DirectoryInfo(pathview + "\\" + modulename);
                        //DirectoryInfo ditemp = new DirectoryInfo(pathziptemp);

                        //try
                        //{
                        //    ditemp.Delete(true);
                        //}
                        //catch (Exception exp)
                        //{
                        //    MessageBox.Show(exp.Message);
                        //}

                        //CopyAll(diview, ditemp);
                        ShowDirectory(diview);

                        for (int j = 0; j < al.Count; j++)
                        {
                            string strsplit = al[j].ToString();
                            char[] c ={ '\\' };
                            string[] str = strsplit.Split(c);
                            ia.adddllname(str[str.Length - 1].ToString(), zipflag);

                            a = System.Reflection.Assembly.LoadFrom(al[j].ToString());
                            Type[] tarray = a.GetTypes();

                            for (int i = 0; i < tarray.Length; i++)
                            {
                                if (tarray[i].BaseType != null)
                                {
                                    if (tarray[i].BaseType.Name == "UserControl")
                                    {
                                        bool f = ia.addnamespace(tarray[i].Module.ToString(), tarray[i].Namespace, tarray[i].Assembly.ImageRuntimeVersion.ToString());
                                        bool cl = ia.addclass(tarray[i].Namespace, tarray[i].Name);
                                    }
                                }
                            }
                        }                        
                        return finaldestination;
                    }
                    else
                    {
                        return "";
                    }
                }
                return "";
            }
            catch (Exception exp)
            {
                //MessageBox.Show("Main Method:" + exp.Message);
                exp.Data.Add("My Key", "1videoConference--:--ZipUnzip.cs--:--uploadfile()--:--");
                clsUtility.LogException = exp;
                return "";
            }
        }

        public void ShowDirectory(DirectoryInfo dir)
        {
            foreach (FileInfo file in dir.GetFiles())
            {
                int hj = al.Add(file.FullName);
            }
            foreach (DirectoryInfo subDir in dir.GetDirectories())
            {
                ShowDirectory(subDir);
            }
        }

        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            if (Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);
            }
            foreach (FileInfo fi in source.GetFiles())
            {
                fi.CopyTo(System.IO.Path.Combine(target.ToString(), fi.Name), true);
            }

            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {   
                DirectoryInfo[] ditar = target.GetDirectories();
                for (int i = 0; i < ditar.Length; i++)
                {                    
                    if (ditar[i].Name == diSourceSubDir.Name)
                    { }
                    else
                    {
                        DirectoryInfo nextTargetSubDir1 = target.CreateSubdirectory(diSourceSubDir.Name);
                        CopyAll(diSourceSubDir, nextTargetSubDir1);
                    }
                }
                if (ditar.Length.ToString() == "0")
                {
                    DirectoryInfo nextTargetSubDir1 = target.CreateSubdirectory(diSourceSubDir.Name);
                    CopyAll(diSourceSubDir, nextTargetSubDir1);
                }
            }
        }
    }
}
